﻿using System.Collections.ObjectModel;
using System.Linq;
using CI.Models;
using CI.Models.Data;
using CI.Models.Production;
using CI.Models.Production.Interfaces;
using CI.Services;
using Microsoft.Practices.ServiceLocation;

namespace CI.ViewModels
{
    public class Johnny5Character : ICharacter
    {
        private readonly IStaticDataRepository<Item> _repo =
            ServiceLocator.Current.GetInstance<IStaticDataRepository<Item>>();

        public Johnny5Character()
        {
            ID = 555;
            Name = "Johnny 5";
            Type = OwnerType.Character;
            RequestedSkills = new ObservableCollection<ISkill>();
        }

        public int ID { get; private set; }
        public string Name { get; private set; }
        public OwnerType Type { get; private set; }
        public ObservableCollection<ISkill> RequestedSkills { get; private set; }

        public ISkill GetSkill(string skillName)
        {
            var preReq = RequestedSkills.Where(s => s.SkillItem.TypeName == skillName).FirstOrDefault();

            if (preReq == null)
            {
                var item = _repo.All.Where(i => i.TypeName == skillName).FirstOrDefault();

                if (item == null) return null;

                var newSkill = new Skill {SkillItem = item, Level = 5};

                RequestedSkills.Add(newSkill);

                preReq = newSkill;
            }

            return preReq;
        }

        public ISkill GetSkill(int skillID)
        {
            var preReq = RequestedSkills.Where(s => s.SkillItem.TypeID == skillID).FirstOrDefault();

            if (preReq == null)
            {
                var item = _repo.All.Where(i => i.TypeID == skillID).FirstOrDefault();

                if (item == null) return null;

                var newSkill = new Skill {SkillItem = item, Level = 5};

                RequestedSkills.Add(newSkill);

                preReq = newSkill;
            }

            return preReq;
        }
    }
}